package com.fast.controller.delay;

import com.fasterxml.jackson.annotation.JsonFormat;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @author hujw
 * @since 2022/1/20
 */
public class Order implements Delayed {

    public String name;
    public String nsrsbh;
    /**
     * 延迟时间
     */
    @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private long time;


    public Order(String name, long time, TimeUnit unit,String nsrsbh) {
        this.name = name;
        this.nsrsbh = nsrsbh;
        this.time = System.currentTimeMillis() + (time > 0 ? unit.toMillis(time) : 0);
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return time - System.currentTimeMillis();
    }

    @Override
    public int compareTo(Delayed o) {
        Order Order = (Order) o;
        long diff = this.time - Order.time;
        if (diff <= 0) {
            return -1;
        } else {
            return 1;
        }
    }

//    public static void main(String[] args) throws InterruptedException {
//        Order Order1 = new Order("Order1", 5, TimeUnit.SECONDS);
//        Order Order2 = new Order("Order2", 10, TimeUnit.SECONDS);
//        Order Order3 = new Order("Order3", 15, TimeUnit.SECONDS);
//        DelayQueue<Order> delayQueue = new DelayQueue<>();
//        delayQueue.put(Order1);
//        delayQueue.put(Order2);
//        delayQueue.put(Order3);
//
//        System.out.println("订单延迟队列开始时间:" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
//        while (delayQueue.size() != 0) {
//            /**
//             * 取队列头部元素是否过期
//             */
//            Order task = delayQueue.poll();
//            if (task != null) {
//                System.out.format("订单:{%s}被取消, 取消时间:{%s}\n", task.name, LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
//            }
//            Thread.sleep(1000);
//        }
//
//    }
}
